package maxDistance;

public class Solution {
    public int maxDistance(int[] colors) {
        // 找出每个颜色最左边的索引和最右边的索引
        int[][] marks = new int[101][2];
        for (int i = 0; i <= 100; i++) {
            marks[i][0] = Integer.MAX_VALUE;
            marks[i][1] = Integer.MIN_VALUE;
        }
        for (int i = 0; i < colors.length; i++) {
            marks[colors[i]][0] = Math.min(marks[colors[i]][0],i);
            marks[colors[i]][1] = Math.max(marks[colors[i]][1],i);
        }
        int ans = 0;
        for (int i = 0; i <= 100; i++) {
            for (int j = 0; j <= 100; j++) {
                if(i!=j&&marks[i][0]>=0&&marks[i][1]<colors.length) {
                    ans = Math.max(ans,Math.abs(marks[i][0] - marks[i][1]));
                }
            }
        }
        return ans;
    }
}
